﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;

namespace NJGZBTYHXT
{
    public partial class OpenExcelFile : Form
    {
        #region 属性字段

        private string _fileName;
        /// <summary>
        /// Excel文件名
        /// </summary>
        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

        private string[] _sheetNames;
        /// <summary>
        /// 数据表列表
        /// </summary>
        public string[] SheetNames
        {
            get { return _sheetNames; }
            set { _sheetNames = value; }
        }

        private string _filter;
        /// <summary>
        /// 文件过滤器
        /// </summary>
        public string Filter
        {
            get { return _filter; }
            set { _filter = value; }
        }

        private string _selectedSheetName;
        /// <summary>
        /// 已选数据表
        /// </summary>
        public string SelectedSheetName
        {
            get { return _selectedSheetName; }
            set { _selectedSheetName = value; }
        }

        #endregion

        public OpenExcelFile()
        {
            InitializeComponent();

            _fileName = "";
            _sheetNames = new string[0];
            _filter = "";
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text.Trim() != "")
                {
                    _fileName = textBox1.Text.Trim();
                }
                else
                {
                    MessageBox.Show("请选择数据文件", "");
                    button1.Focus();
                    return;
                }

                if (comboBox1.Text.Trim() != "" || comboBox1.SelectedText.Trim() != "")
                {
                    _selectedSheetName = comboBox1.Text.Trim() != "" ? comboBox1.Text.Trim() : comboBox1.SelectedText;
                }
                else
                {
                    MessageBox.Show("请选择数据表", "");
                    comboBox1.Focus();
                    return;
                }

                ConfigurationManager.AppSettings["dbFile"] = "db" + Path.GetExtension(_fileName);
                ConfigurationManager.AppSettings["dbFileSheetName"] = _selectedSheetName;
                File.Copy(_fileName, Application.StartupPath + @"\db" + Path.GetExtension(_fileName), true);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.HelpLink, "Exception");
            }
        }

        /// <summary>
        /// 浏览按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "选择文件";
            ofd.Filter = this.Filter;

            if (ofd.ShowDialog(this) == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;

                int sheetCount = 0;

                DataSet ds = new DataSet();

                try
                {
                    ds = Common.Excel.NPOIHandler.ExcelToDataSet(ofd.FileName, true, out sheetCount);

                    if (sheetCount > 0)
                    {
                        comboBox1.Items.Clear();

                        _sheetNames = new string[ds.Tables.Count];

                        for (int i = 0; i < ds.Tables.Count; i++)
                        {
                            _sheetNames[i] = ds.Tables[i].TableName;
                            comboBox1.Items.Add(_sheetNames[i]);
                        }

                        comboBox1.SelectedIndex = 0;
                    }
                    else
                    {
                        comboBox1.Items.Add("*没有找到数据表*");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Exception");
                }
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
